<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\ChatUnreadMessageModel;


class RemoveUnreadMessageConsumer extends BaseConsumer
{

    // 要消费的队列名
    public $queue = 'remove_unread_message';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     * @throws \Exception
     */
    public function doConsume($data)
    {
        // 删除数量限制 默认100
        $where = $data['filter'] ?? [];
        $deleteLimit = $data['limit'] ?? 100;

        if (empty($deleteLimit) || empty($where)) {
            return;
        }
        try {
            $chatUnreadMessageModel = model(ChatUnreadMessageModel::class);
            $waitRemoveCount = $chatUnreadMessageModel->where($where)->count();
            for (; $waitRemoveCount > 0; $waitRemoveCount -= $deleteLimit) {
                $chatUnreadMessageModel
                    ->where($where)
                    ->limit($deleteLimit)
                    ->delete();
            }
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
